package pl.ptemich.tankwars.objects;

import java.util.Random;

import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;

public class Paddle {

  /**
   * paddle image 
   */
  private static Image paddleImage;

  private static final float MAX_SPEED_X = 0.5f;
  
  private static final int PADDLE_WIDTH = 30;
  
  private static final int PADDLE_HEIGHT = 5;
  
  private int fieldWidth;  
  
  private float paddlePosX;
  
  private float paddlePosY;
  
  private float paddleSpeedX;
  
  public Paddle(int fieldWidth, int fieldHeight) throws SlickException {
    this.fieldWidth = fieldWidth; 
    
    Random rand = new Random();
    paddlePosX = Double.valueOf(fieldWidth * Math.sin(rand.nextDouble())).floatValue();
    paddlePosY = fieldHeight - PADDLE_HEIGHT;
    
    paddleSpeedX = 0;
       
    if (paddleImage == null) {
      paddleImage = new Image("resources/images/paddle.png");
    }
  }
  
  public void render(Graphics graphics)
      throws SlickException {
    paddleImage.draw(paddlePosX - PADDLE_WIDTH / 2, paddlePosY);
  }  
  
  public void update(int delta) {
    paddlePosX += delta * paddleSpeedX;
    
    if (paddlePosX - PADDLE_WIDTH / 2 < 0) {
      // left wall
      paddlePosX = PADDLE_WIDTH / 2;     
    }
    
    if (paddlePosX + PADDLE_WIDTH / 2 > fieldWidth) {
      // right wall
      paddlePosX = fieldWidth - PADDLE_WIDTH / 2;      
    }  
    
    paddleSpeedX = 0;
  }
  
  public void moveLef() {
    paddleSpeedX = -MAX_SPEED_X;
  }
  
  public void moveRight() {
    paddleSpeedX = MAX_SPEED_X;
  }

}
